import Vue from 'vue'
import CryptoJS from 'crypto-js'
var aes_key = 'xlen5Px9V6rjzi4z'
export default { //加密
	encrypt(word, keyStr) {
		if(keyStr == undefined){
			keyStr = aes_key;
		}
		console.log(keyStr)
		var key = CryptoJS.enc.Utf8.parse(keyStr); //Latin1 w8m31+Yy/Nw6thPsMpO5fg==
		var srcs = CryptoJS.enc.Utf8.parse(word);
		var encrypted = CryptoJS.AES.encrypt(srcs, key, {
			mode: CryptoJS.mode.ECB,
			padding: CryptoJS.pad.Pkcs7
		});
		// return encrypted.toString();
		return this.createRandomNumber()+CryptoJS.enc.Base64.stringify(encrypted.ciphertext)+word
	},
	encryptData(word, keyStr) {
		if(keyStr == undefined){
			keyStr = aes_key;
		}
		console.log(keyStr)
		var key = CryptoJS.enc.Utf8.parse(keyStr); //Latin1 w8m31+Yy/Nw6thPsMpO5fg==
		var srcs = CryptoJS.enc.Utf8.parse(word);
		var encrypted = CryptoJS.AES.encrypt(srcs, key, {
			mode: CryptoJS.mode.ECB,
			padding: CryptoJS.pad.Pkcs7
		});
		// return encrypted.toString();
		return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
	},
	//解密
	decrypt(word, keyStr) {
		if(keyStr == undefined){
			keyStr = aes_key;
		}
		var key = CryptoJS.enc.Utf8.parse(keyStr); //Latin1 w8m31+Yy/Nw6thPsMpO5fg==
		var decrypt = CryptoJS.AES.decrypt(word, key, {
			mode: CryptoJS.mode.ECB,
			padding: CryptoJS.pad.Pkcs7
		});
		return CryptoJS.enc.Utf8.stringify(decrypt).toString();
	},
	//生成随机数
	createRandomNumber() {
		var str = "T";
		var arr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
		
		for(var i=0; i<15; i++){
			var index = Math.round(Math.random()*(arr.length - 1));
			str += arr[index];
		}
		return str;
	}
}
